#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "Deck.h"
#include "hand.h"

using namespace std;

int winnings (handType pay, int bet);
int bets (int amt);

int main ()
{
		srand(time(NULL));
        Deck playerDeck;
        string keepCards;
        char playAgain = 'Y';
		int total = 1000;
		int monwon = 0;
		int monlost = 0;
		int wins = 0;
		int losses = 0;


        cout<< "Would you like to load a previous game?(Y/N): ";
				char response;
				cin>>response;
                                cout << endl;

				if (toupper(response) == 'Y')
				{
					ifstream infile ("data.txt");

					if(infile)
						infile>>total >>monwon >>monlost >>wins >>losses;

					infile.close();
				}


	do
        {

                Hand tempHand;
                Hand playerHand;
                int bet = bets(total);

				if (playerDeck.leftIn() <= 6)
					playerDeck.shuffle();
					
                handType payout = NOHAND;
                int discardedAmt = 5;
				
				for (int b = 0; b < 5; b++)
				{
					tempHand.addCard(playerDeck.draw());
				}
				
                tempHand.displayHand();

                //------------------------------DISCARD PHASE----------------------------------------
                cout << "  A\t    B\t      C\t        D\t  E" << endl;
                cout << "Please enter the letter(s), without spaces, corresponding to the cards you\nwould like to keep. Enter N if you would like to discard the entire hand: ";
                cin  >> keepCards;

                        if (keepCards.length() != 0)
                        {
                                for (int a = 0; a < keepCards.length(); a++)
                                {
                                    keepCards[a] = toupper(keepCards[a]);
                                }


	                for (int i = 0; i < keepCards.length() && i < 5; i++)
	                {
	                        switch (keepCards[i])
	                        {
	                                case 'A':
	                                        playerHand.addCard(tempHand.getCard(0));
	                                        discardedAmt--;
	                                        break;
	                                case 'B':
	                                        playerHand.addCard(tempHand.getCard(1));
	                                        discardedAmt--;
	                                        break;
	                                case 'C':
	                                        playerHand.addCard(tempHand.getCard(2));
	                                        discardedAmt--;
	                                        break;
	                                case 'D':
	                                        playerHand.addCard(tempHand.getCard(3));
	                                        discardedAmt--;
	                                        break;
	                                case 'E':
	                                        playerHand.addCard(tempHand.getCard(4));
	                                        discardedAmt--;
	                                        break;
	                                default:
	                                        break;
	                        }
	                }
                     }

                //------------------------------END DISCARD PHASE----------------------------------------

                //------------------------------DRAW & DECIDE PHASE----------------------------------------
                for (int c = 0; c < discardedAmt; c++)
                {
                        playerHand.addCard(playerDeck.draw());
                }

                cout << playerHand;

                payout = playerHand.findHand();
				int mon = winnings(payout, bet);
				if (mon > 0)
				{
					cout<< "You have won $" <<mon <<"!\n";
					monwon += mon;
					total += mon;
					wins++;
				}
				else
				{
					cout<< "You have lost $" <<bet <<".\n";
					monlost += bet;
					total -= bet;
					losses++;
				}
                

                cout << "Would you like to play again?(y/n): ";
                cin >> playAgain;
                cout << endl;
                system("cls");

        }while(toupper(playAgain) == 'Y');

		char resp;
		cout<< "Would you like to save your game? Y/N" <<endl;
		cin>>resp;

		if (toupper(resp) == 'Y')
		{
			ofstream outfile("data.txt");
			outfile<< total <<" " <<monwon <<" " <<monlost <<" " <<wins <<" " <<losses;
			outfile.close();
		}


	return 0;
}

int bets (int total)
{
	int resp;
	cout <<"You may bet between $10 and $500. You currently have $" <<total <<".\n" << "How much would you like to bet? >: ";
	cin>> resp;
        cout << endl;

	if (resp >= 10 && resp <= 500 && resp <= total)
		return resp;
	else
		return 0;
}

int winnings (handType pay, int bet)
{
	if (pay == PAIR)
		return bet;
	else if (pay == TWO_PAIR)
		return bet*2;
	else if (pay == THREE_KIND)
		return bet*3;
	else if (pay == STRAIGHT)
		return bet*4;
	else if (pay == FLUSH)
		return bet*6;
	else if (pay == FULL_HOUSE)
		return bet*9;
	else if (pay == FOUR_KIND)
		return bet*25;
	else if (pay == STRAIGHT_FLUSH)
		return bet*100;
	else 
		return 0;
}